//###########################################################################
// main.rs
// The specific implementation of functions related to main
//
// $Copyright: Copyright (C) village
//###########################################################################
#![no_std]
#![no_main]

// import alloc
extern crate alloc;

// import village
pub mod village;
pub use village::traits as traits;
pub use village::misc as misc;

use crate::traits::driver::IOCmd;
use crate::misc::devopt::Devopt;

// Main
#[unsafe(no_mangle)]
pub fn main(_argv: &[&str]) {
    // 0. 获取display驱动并打开
    println!("\n=== 0. 打开驱动 ===");
    let mut fbdev = Devopt::new();
    if !fbdev.open("display0") {
        println!("打开display驱动失败");
        return;
    }
    println!("驱动已打开");

    // 1. 获取信息
    println!("\n=== 1. 获取信息 ===");
    let mut fbcmd = IOCmd::fb_info_cmd();
    fbdev.ioctrl(&mut fbcmd);
    if let Some((w, h, bpp)) = fbcmd.fb_info_val() {
        println!("分辨率: {} x {}, 位深: {}", w, h, bpp);
    }
    
    // 2. 清除屏幕
    println!("\n=== 2. 清除屏幕 ===");
    let mut fbcmd = IOCmd::fb_clear_cmd(0xFFFF);
    fbdev.ioctrl(&mut fbcmd);
    println!("屏幕已清除");

    // 3. 绘制点
    println!("\n=== 3. 绘制点 ===");
    let mut fbcmd = IOCmd::fb_draw_point_cmd(100, 100, 0x1234);
    fbdev.ioctrl(&mut fbcmd);
    if let Some((x, y, color)) = fbcmd.fb_draw_point_val() {
        println!("在坐标({}, {})绘制了颜色为0x{:X}的点", x, y, color);
    }

    // 4. 读取点
    println!("\n=== 4. 读取点 ===");
    let mut fbcmd = IOCmd::fb_read_point_cmd(100, 100);
    fbdev.ioctrl(&mut fbcmd);
    if let Some((x, y, color)) = fbcmd.fb_read_point_val() {
        println!("坐标({}, {})的颜色是0x{:X}", x, y, color);
    }
  
    // 5. 填充颜色
    println!("\n=== 5. 填充颜色 ===");
    let mut fbcmd = IOCmd::fb_fill_color_cmd(50, 50, 150, 150, 0xFF00);
    fbdev.ioctrl(&mut fbcmd);
    if let Some((sx, sy, ex, ey, color)) = fbcmd.fb_fill_color_val() {
        println!("在矩形区域[({}, {}), ({}, {})]填充了颜色0x{:X}", sx, sy, ex, ey, color);
    }
    
    // 6. 填充像素
    println!("\n=== 6. 填充像素 ===");
    let mut fbcmd = IOCmd::fb_fill_pixel_cmd(200, 200, 210, 210, alloc::vec![0x1234; 100]);
    fbdev.ioctrl(&mut fbcmd);
    if let Some((sx, sy, ex, ey, pixel)) = fbcmd.fb_fill_pixel_val() {
        println!("在矩形区域[({}, {}), ({}, {})]填充了{}个像素数据", sx, sy, ex, ey, pixel.len());
    }
}
